package com.cy.ppj.web.admin.user;

import com.cy.ppj.service.account.TeamCommissionService;
import com.cy.ppj.service.dealrecord.DealRecordService;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cy.ppj.comm.constant.Constants;
import com.cy.ppj.model.PageBean;
import com.cy.ppj.model.Result;
import com.cy.ppj.model.ao.user.ChannelUserAddAO;
import com.cy.ppj.model.dto.user.UserAddDTO;
import com.cy.ppj.model.enums.UserStatus;
import com.cy.ppj.model.enums.UserType;
import com.cy.ppj.model.query.UserAdminQuery;
import com.cy.ppj.model.query.account.TeamMemberQuery;
import com.cy.ppj.model.query.DealRecordAdminQuery;
import com.cy.ppj.model.query.OrderAdminByTypeQuery;
import com.cy.ppj.model.vo.ChannelUserAdminVO;
import com.cy.ppj.model.vo.OrderAdminByTypeVO;
import com.cy.ppj.model.vo.UserDetailAdminVO;
import com.cy.ppj.model.vo.admin.teamcommission.TeamMemberAdminVO;
import com.cy.ppj.model.vo.admin.teamcommission.TeamMemberCountStatisticsVO;
import com.cy.ppj.model.vo.admin.dealrecord.DealRecordAdminVO;
import com.cy.ppj.service.order.OrderService;
import com.cy.ppj.service.user.UserService;
import com.cy.ppj.web.BaseController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

/**
 * 后台-渠道员（经纪人）管理
 * @author: tanyilin
 * @create: 2018年7月31日 下午5:28:31
 */
@Slf4j
@RestController
@RequestMapping("/admin/user/channel")
@Api(tags = "后台-（经纪人）管理")
public class AdminChannelUserController extends BaseController{

	@Autowired
	private UserService userService;
	@Autowired
	private OrderService orderService;
	@Autowired
	private DealRecordService dealRecordService;
	@Autowired
	private TeamCommissionService teamCommissionService;
	
	@ApiOperation("（经纪人）列表")
	@GetMapping("/list")
	public PageBean<ChannelUserAdminVO> list(UserAdminQuery channelUserAdminQuery) {
		PageBean<ChannelUserAdminVO> pageBean = userService.queryChannelUserList(channelUserAdminQuery);
		return pageBean;
	}
	
	@ApiOperation("渠道员（经纪人）信息详情")
	@GetMapping("/detail")
	public Result detail(@ApiParam(value = "用户ID", example = "0") @RequestParam Long userId) {
		UserDetailAdminVO userDetailAdminVO = userService.queryUserDetailAdmin(userId);
		return ok("查询成功", userDetailAdminVO);
	}
	 
	@ApiOperation("渠道员资金记录")
	@GetMapping("/deal/records")
	public PageBean<DealRecordAdminVO> realRecords(DealRecordAdminQuery query) {
		PageBean<DealRecordAdminVO> pageBean = dealRecordService.queryDealRecordAdmin(query);
		return pageBean;
	}
	
	@ApiOperation("经纪人个人订单明细")
	@GetMapping("/order/list")
	public PageBean<OrderAdminByTypeVO> orderList(OrderAdminByTypeQuery query) {
		query.setType(1);
		PageBean<OrderAdminByTypeVO> pageBean = orderService.queryOrderAdminByType(query);
		return pageBean;
	}
	
	@ApiOperation("经纪人的团队订单明细")
	@GetMapping("/team/order/list")
	public PageBean<OrderAdminByTypeVO> teamOrderList(OrderAdminByTypeQuery query) {
		query.setType(2);
		PageBean<OrderAdminByTypeVO> pageBean = orderService.queryOrderAdminByType(query);
		return pageBean;
	}
	
	@ApiOperation("更改用户状态")
	@PostMapping("/update/status")
	public Result updateStatus(@ApiParam(value = "用户ID", example = "0", required = true) @RequestParam Long userId,
			@ApiParam(value = "用户状态", required = true) @RequestParam UserStatus userStatus) {
		userService.updateUserStatus(userId, userStatus);
		return ok();
	}
	
	@ApiOperation("解绑银行卡")
	@PostMapping("/unbind/bank")
	public Result unbindBank(@ApiParam(value = "用户ID", example = "0", required = true) @RequestParam Long userId) {
		userService.unBindBank(userId);
		return ok();
	}
	
	@ApiOperation("团队人数列表")
	@GetMapping("/my/team/members")
	public PageBean<TeamMemberAdminVO> myTeamMembers(TeamMemberQuery query) {
		PageBean<TeamMemberAdminVO> pageBean = teamCommissionService.selectTeamMemberAdmin(query);
		return pageBean;
	}
	
	@ApiOperation("团队人数统计")
	@GetMapping("/my/team/members/statistics")
	public Result<TeamMemberCountStatisticsVO> myTeamMembersCountStatistics(@ApiParam(value = "用户ID", example = "0", required = true) @RequestParam Long userId) {
		TeamMemberCountStatisticsVO vo = teamCommissionService.myTeamMembersCountStatistics(userId);
		return ok("", vo);
	}
	
	@ApiOperation("新增经纪人")
	@PostMapping("/do/add")
	public Result doAddNewChannelUser(@Validated ChannelUserAddAO addAo, BindingResult bindingResult) {
		this.checkError(bindingResult);
		UserAddDTO userAddDTO = new UserAddDTO();
		BeanUtils.copyProperties(addAo, userAddDTO);
        userAddDTO.setUserType(UserType.SALES);
        userAddDTO.setPassword(Constants.USER_INIT_PASSWORD);
        userService.insertUser(userAddDTO);
		return ok();
	}
	
}
